package com.ibix.print;

import com.ibix.constant.OtherInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.awt.*;
import java.awt.print.*;
import java.util.NoSuchElementException;

/**
 * 项目名称：YiDianCan
 * <br>类描述：
 * <br>创建人：htliu
 * <br>创建时间：2018/2/5 10:41
 * <br>修改人:
 * <br>修改时间：2018/2/5 10:41
 * <br>修改备注：
 *
 * @author htliu
 * @date 2018/2/5 10:41
 */
public class PrintUtil implements Printable {
    private static final Logger LOGGER = LoggerFactory.getLogger(PrintUtil.class);
    private PrintBean printBean;

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex > 0 || printBean.itemList().isEmpty()) {
            return NO_SUCH_PAGE;
        }
        Graphics2D g2d = (Graphics2D) graphics;
        for (PrintBean.Item item : printBean.itemList()) {
            g2d.setFont(new Font("Default", Font.PLAIN, 12));
            String content = item.content();
            int x = item.x();
            int y = item.y();
            g2d.drawString(content, x, y);
        }
        return PAGE_EXISTS;
    }

    /**
     * @param printBean
     * @throws NoSuchElementException 没有找到对应的打印机设备
     */
    public void print(PrintBean printBean) throws NoSuchElementException, PrinterException {
        this.printBean = printBean;
        int height = printBean.itemList().size() * 30 + 3 * 15 + 20 + printBean.extraHeight();
        // 通俗理解就是书、文档
        Book book = new Book();
        // 打印格式
        PageFormat pf = new PageFormat();
        pf.setOrientation(PageFormat.PORTRAIT);

        // 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
        Paper p = new Paper();
        p.setSize(230, height);
        p.setImageableArea(2, -20, 230, height + 20);
        pf.setPaper(p);

        // 把 PageFormat 和 Printable 添加到书中，组成一个页面
        book.append(this, pf);

        // 获取打印服务对象
        PrinterJob job = getPrinterJob(printBean.printName());
        if (job == null) {
            LOGGER.info("printOrder:  没有找到对应的打印机设备");
            throw new NoSuchElementException("没有找到对应的打印机设备");
        }
        job.setPageable(book);
        job.print();
    }

    private PrinterJob getPrinterJob(String name) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
     /*   for (PrintService printService : PrinterJob.lookupPrintServices()) {
            if (name.equals(printService.getName())) {
                try {
                    printerJob.setPrintService(printService);
                } catch (PrinterException e) {
                    e.printStackTrace();
                }
                return printerJob;
            }
        }*/
        switch (name)
        {
            case OtherInfo.PRINT_HOT:
                return PrintJobPack.getCallFoodPrint();
            case OtherInfo.PRINT_CASHIER:
                return PrintJobPack.getCashierPrint();
            case OtherInfo.PRINT_COLD:
                return PrintJobPack.getCookPrint();
            default:
                return null;
        }
    }
}
